package heartBeat;

import java.io.File;
import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.rmi.RemoteException;
import java.sql.SQLException;
import java.util.Properties;
import java.util.TimerTask;

import dataStore.DownloadFromFtp;
import dataStore.FtpServerApiInterface;
import dataStore.SlaverDataStoreApiInterface;
import dataStore.UploadToFtp;
import dataStruct.EnumHeartBeatType;
import dataStruct.HeartBeatResponse;
import dataStruct.SlaverLoad;

public class HeartBeatTask extends TimerTask{

	public HeartBeatTask(MasterHeartBeatApiInterface masterHBApi, SlaverDataStoreApiInterface masterDSApi, FtpServerApiInterface ftpServerApi, Properties slaverProper) throws RemoteException, UnknownHostException, SQLException
	{
		HeartBeatTask.masterHBApi = masterHBApi;
		SlaverLoad slaverLoad = HeartBeatTask.get_slaver_load();
		
		HeartBeatResponse heartBeatResponse = masterHBApi.heartBeat(
				InetAddress.getLocalHost().hashCode(),
				EnumHeartBeatType.REQUEST_REGISTER,
				slaverLoad,
				slaverProper.getProperty("slaverWorkplace"));
		System.out.println(heartBeatResponse.info);
		
		HeartBeatTask.downloadFromFtp = new DownloadFromFtp(masterDSApi);
		HeartBeatTask.uploadToFtp = new UploadToFtp(masterDSApi, ftpServerApi);
	}
	public void run() {
		try {
			SlaverLoad slaveLoad =get_slaver_load();
			HeartBeatResponse heartBeatResponse = masterHBApi.heartBeat(
					InetAddress.getLocalHost().hashCode(), 
					EnumHeartBeatType.REQUEST_ALIVE,
					slaveLoad,
					"I am alive, give me task please!");
			
			switch(heartBeatResponse.heartBeatType)
			{
			case RESPONSE_RELAX: 
				System.out.println(heartBeatResponse.info);
				break;
				
			case RESPONSE_UPLOAD_FILE:
				System.out.println(String.format("response : RESPONSE_UPLOAD_FILE ,taskId = %s", heartBeatResponse.info));
				uploadToFtp.upload_to_ftp(heartBeatResponse.info);
				break;
				
			case RESPONSE_UPLOAD_WORDCOUNT_FILE:
				System.out.println(String.format("response : RESPONSE_UPLOAD_FILE ,taskId = %s", heartBeatResponse.info));
				uploadToFtp.wordcount_upload_to_ftp(heartBeatResponse.info);
				break;
				
			case RESPONSE_DOWNLOAD_FILE:
				System.out.println(String.format("response : RESPONSE_DOWNLOAD_FILE ,taskId = %s", heartBeatResponse.info));
				downloadFromFtp.download_from_ftp(heartBeatResponse.info);
				
				break;
//			case EnumHeartBeatType.RE
			default: break; 
			}
		} catch (RemoteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public static SlaverLoad get_slaver_load()
	{
		long memTotal = 0, memFree = 0;
		File[] roots = File.listRoots();//获取磁盘分区列表
		for (File file : roots) {
//		System.out.println(file.getPath() + "信息如下:");
		memTotal += file.getTotalSpace()  / 1024 / 1024 ;//M空闲空间
		memFree += file.getFreeSpace()  / 1024 / 1024 ;
		}
		SlaverLoad slaverLoad = new SlaverLoad(memTotal, memFree);
		return slaverLoad;
	}
	
	
	private static MasterHeartBeatApiInterface masterHBApi;
	private static DownloadFromFtp downloadFromFtp;
	private static UploadToFtp uploadToFtp;
}
